package com.ele.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ele.pojo.Cart;
import com.ele.pojo.Food;
import com.ele.pojo.ResponseData;
import com.ele.service.CartService;
import com.ele.mapper.CartMapper;
import com.ele.service.FoodService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* @author 28444
* @description 针对表【cart】的数据库操作Service实现
* @createDate 2024-04-10 09:21:03
*/
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
    implements CartService{
    @Resource
    private FoodService foodService;
    @Override
    public ResponseData<Void> addCart(Cart cart) {
        ResponseData<Void> responseData = new ResponseData<>();
        boolean save = this.save(cart);
        if(save){
            responseData.setCode(20000);
            responseData.setMessage("购物车添加成功");
        }else{
            responseData.setCode(50000);
            responseData.setMessage("购物车添加失败");
        }
        return responseData;
    }

    @Override
    public ResponseData<Void> updateCart(Cart cart) {
        ResponseData<Void> responseData = new ResponseData<>();
        LambdaUpdateWrapper<Cart> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Cart::getUserId,cart.getUserId())
                .eq(Cart::getFoodId,cart.getFoodId())
                .set(Cart::getQuantity,cart.getQuantity());
        boolean update = this.update(wrapper);
        if(update){
            responseData.setCode(20000);
            responseData.setMessage("购物车修改成功");
        }else{
            responseData.setCode(50000);
            responseData.setMessage("购物车修改失败");
        }
        return responseData;
    }

    @Override
    public ResponseData<Cart> getCartData(String userId) {
        ResponseData<Cart> responseData = new ResponseData<Cart>();
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Cart> list = this.list(wrapper);
        if(list.size()>0){
            // 需要根据购物车的数据，查询出对应的菜品信息
            list.forEach(cart ->{
                Food food = foodService.getById(cart.getFoodId());
                cart.setFood(food);
            });

            responseData.setCode(20000);
            responseData.setDatas(list);
        }else{
            responseData.setCode(50000);
            responseData.setMessage("您还没有选择商品");
        }
        return responseData;
    }

    @Override
    public ResponseData<Void> deleteCartByIds(String[] ids) {
        ResponseData<Void> responseData = new ResponseData<Void>();
        // 需要将ids数组转成集合
        List<String> list = Arrays.asList(ids);
        boolean remove = this.removeBatchByIds(list);
        if(remove){
            responseData.setCode(20000);
            responseData.setMessage("购物车删除成功");
        }else{
            responseData.setCode(50000);
            responseData.setMessage("购物车删除失败");
        }
        return responseData;
    }

    @Override
    public ResponseData<Void> updateCartById(Cart cart) {
        ResponseData<Void> responseData = new ResponseData<Void>();
        LambdaUpdateWrapper<Cart> wrapper = new LambdaUpdateWrapper<Cart>();
        wrapper.eq(Cart::getCartId,cart.getCartId())
                .set(Cart::getQuantity,cart.getQuantity());
        boolean update = this.update(wrapper);
        if(update){
            responseData.setCode(20000);
            responseData.setMessage("购物车修改成功");
        }else{
            responseData.setCode(50000);
            responseData.setMessage("购物车修改失败");
        }
        return responseData;
    }

    @Override
    public ResponseData<Cart> getCheckCart(String[] ids) {
        ResponseData<Cart> responseData = new ResponseData<Cart>();
        List<Cart> carts = this.listByIds(Arrays.asList(ids));
        if(carts != null && carts.size() > 0){
            carts.forEach(cart ->{
                Food food = foodService.getById(cart.getFoodId());
                cart.setFood(food);
            });

            responseData.setCode(20000);
            responseData.setDatas(carts);
        }else{
            responseData.setCode(50000);
            responseData.setMessage("没有数据");
        }
        return responseData;
    }
}